<?php
namespace Common\Model;
use Common\Model\VRNewsModel;
use Admin\Controller\Tencentyun\Conf;
use Admin\Controller\Tencentyun\ImageV2;
class NewsModel extends VRNewsModel
{
	
	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
		array('offer_price', 'require', '出价不能为空！', 0, 'regex', CommonModel:: MODEL_BOTH  ),
	);

	/**
	 * 上传图片
	 */
	public function uploadImage(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     59614161 ;// 设置附件上传大小
		$upload->exts      =     array('jpeg', 'jpg', 'bmp', 'png');// 设置附件上传类型
		$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录


		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			return $upload->getError();
			//$this->error($upload->getError());
		}else{
			if(count($info) == 1){
				$count = $this->getmaxdim($info);
				if($count >= 2){
					foreach ($info as $key=>$value){
						$info_news = $value;
					}
//					var_dump($info_news);exit();
					unset($info);
					$info['imageFile'] = $info_news;
				}

				$cname = C('UPLOADPATH').$info['imageFile']['savepath'].$info['imageFile']['savename'];
				$fileName = 'lv'.time().$info['imageFile']['savename'];  // 自定义文件名
				$uploadRet = ImageV2::upload($cname, Conf::BUCKET, $fileName);
// 				print_r($uploadRet);exit;
				\Think\Log::record('上传封面万象优图返回：'.json_encode($uploadRet), 'INFO');
				if($uploadRet['httpcode'] == 200){
					
					$imageUrl = $uploadRet['data']['downloadUrl'];
					//删除本地文件

					unlink($cname);
					return $imageUrl;
				}
			}else{
				foreach($info as $key=>$val){
					$cname = C('UPLOADPATH').$val['savepath'].$val['savename'];
					$fileName = 'lv'.time().$val['savename'];  // 自定义文件名
					$uploadRet = ImageV2::upload($cname, Conf::BUCKET, $fileName);
// 					print_r($uploadRet);exit;
					\Think\Log::record('上传封面万象优图返回：'.json_encode($uploadRet), 'INFO');
					if($uploadRet['httpcode'] == 200){
						$imageUrl[] = $uploadRet['data']['downloadUrl'];
						//删除本地文件

						unlink($cname);
					}
				}
				return $imageUrl;
			}

		}
	}

	function getmaxdim($arr){

		if(!is_array($arr)){

			return 0;

		}else{

			$dimension = 0;

			foreach($arr as $item1){

				$t1=$this->getmaxdim($item1);

				if($t1>$dimension){$dimension = $t1;}

			}
			return $dimension+1;
		}

	}
	
	//获取专题下的新闻列表
	function getNewsListBySpecialId($special_id = null, $formParam, $pageStart, $pageSize){
		$result_arr = array();
		$where = array();
		if($special_id){
			$where['b.news_id'] = array('eq',$special_id);
			$where['b.status'] = '1';
			$where['a.special_news_id'] = array('eq',$special_id);
			$where['a.title'] = array('neq','');
			$where['a.status'] = array('eq','0');
			if($formParam['keyword']) $where['a.title'] = array('like','%'.$formParam['keyword'].'%');
			if($formParam['source']) $where['a.source_name'] = array('eq',$formParam['source']);
			if($formParam['editorName']) $where['a.creator_name'] = array('eq',$formParam['editorName']);
			if($formParam['startDate']) $where['a.create_time'] =array('egt',$formParam['startDate']);
			if($formParam['endDate']) $where['a.create_time'] = array('elt',$formParam['endDate']);
			if($formParam['weight']) $where['a.weight'] = array('eq',$formParam['weight']);
			if($formParam['type']) $where['a.type'] = array('eq',$formParam['type']);

			$special_type = new SpecialColumnTypeModel();
			$con = $special_type->alias("b")->join("vrnews1.news as a on a.type_id = b.type_id ",'left')
				->join("vrnews1.news_video as c on a.news_id = c.news_id ",'left')
				->where($where)
				->count();
			
			$arr_all = $special_type->alias("b")->join("vrnews1.news as a on a.type_id = b.type_id ",'left')
				->join("vrnews1.news_video as c on a.news_id = c.news_id ",'left')
				->where($where)
//				->where("special_column_type.news_id = $special_id ")
				->field("a.*,b.name as column_name,year(a.create_time) as year1,month(a.create_time) as month1,day(a.create_time) as day1,year(from_unixtime(a.refresh_time)) as year,month(from_unixtime(a.refresh_time)) as month,day(from_unixtime(a.refresh_time)) as day,from_unixtime(a.refresh_time) as refresh_time,c.status as video_status, c.category as video_category")
				/* ->Order("
						case  when year is null then year1 else year end desc,
            			case  when month is null then month1 else month end desc,
            			case  when day is null then day1 else day end desc,
						a.weight desc,
						a.refresh_time desc,
						a.create_time desc,
						b.create_time desc,
						a.update_time desc,
						b.weight desc
						") */
				->Order("
						b.weight desc,
						case  when year is null then year1 else year end desc,
            			case  when month is null then month1 else month end desc,
            			case  when day is null then day1 else day end desc,
						a.weight desc,
						a.refresh_time desc,
						a.create_time desc
				")
				->limit($pageStart, $pageSize)
				->select();
//			if($special_id == 14758324788) print_r($special_type->getLastSql());die;
			//$con = count($arr_all);
			$result_arr['data'] = $arr_all;
			$result_arr['count_Total'] = $con;



//			$where['a.special_news_id'] = array('eq',$special_id);
//			$where['b.status'] = '1';
//			if($formParam['keyword']) $where['a.title'] = array('like','%'.$formParam['keyword'].'%');
//			if($formParam['source']) $where['a.source_name'] = array('eq',$formParam['source']);
//			if($formParam['editorName']) $where['a.creator_name'] = array('eq',$formParam['editorName']);
//			if($formParam['startDate']) $where['a.create_time'] =array('egt',$formParam['startDate']);
//			if($formParam['endDate']) $where['a.create_time'] = array('elt',$formParam['endDate']);
//			if($formParam['weight']) $where['a.weight'] = array('eq',$formParam['weight']);
//			if($formParam['type']) $where['a.type'] = array('eq',$formParam['type']);
//
//			$count_Total = $this->alias("a")->join("vrnews1.special_column_type b on a.type_id = b.type_id","right")->where($where)->count();
//			$news_list = $this
//			->alias("a")
//			->join("vrnews1.special_column_type b on a.type_id = b.type_id")
//			->where($where)
//			->field('a.*,b.name as column_name')
//			->order("b.weight desc,b.type_id desc,a.weight desc,a.refresh_time desc,a.create_time desc")
//			->limit($pageStart, $pageSize)
//			->select();
////			print_r($this->getLastSql());die;
//			$result_arr['data'] = $news_list;
//			$result_arr['count_Total'] = $count_Total;
		}
		return $result_arr;
	}

	
	//设置入口
	function entryAdd($news_id = null, $special_id = null){
		if($news_id && $special_id){
			//special_id  是专题ID，news_id 是 新闻ID
			//查看 此新闻信息
			$news_info = $this->find($news_id);
			if($news_info['full_status'] == 1){
				//通栏
				$data['special_title']    = $news_info['full_title'];
				$data['special_abstract'] = $news_info['full_subtitle'];
				$data['special_image']    = $news_info['full_cover_image'];
				$data['special_type']     = 8;
			}else{
				//判断是否图集  是图集返回content  否 返回cover_image
				if($news_info['type'] == 5){
					$data['special_image']    = $news_info['content'];
				}else{
					$data['special_image']    = $news_info['cover_image'];
				}
				$data['special_title']    = $news_info['title'];
				$data['special_abstract'] = $news_info['subtitle'];
				$data['special_type']     = $news_info['type'];
			}

			//将已被设置为 入口的 去掉状态
			$already_is = $this->where("special_news_id = $special_id and special_entry=1")->find();
			if($already_is){
				$data2['special_entry'] = '0';
				$this->where("special_news_id = $special_id and special_entry=1")->data($data2)->save();
			}

			//设置专题 冗余字段
			$data['special_id'] = $news_id;
			$res_data = $this->where('news_id = '.$special_id)->data($data)->save();
			//入口新闻 加入口状态
			$data1['special_entry'] = '1';
			$res_data1 = $this->where('news_id = '.$news_id)->data($data1)->save();


			if($res_data && $res_data1){
				return true;
			}else{
				return false;
			}


//			$data['special_entry'] = '0';
//			if(($this->where('special_news_id = '.$special_id)->data($data)->save()) !==false){
//				$data1['special_id'] = $news_id;
//				if(($this->where('news_id = '.$special_id)->data($data1)->save()) !==false){
//					$data2['special_entry'] = '1';
//					if(($this->where('news_id = '.$news_id)->data($data2)->save()) !==false){
//						return true;
//					}
//				}
//			}
		}else{
			return false;
		}
	}
	
	//取消入口
	function entryDel($news_id = null, $special_id = null){
		if($news_id && $special_id){
			$data['special_id']      = '0';
			$data['special_title']    = '';
			$data['special_abstract'] = '';
			$data['special_image']    = '';
			$data['special_type']     = '0';

			if(($this->where('news_id = '.$special_id)->data($data)->save()) !==false){
				$data1['special_entry'] = '0';
				if(($this->where('news_id = '.$news_id)->data($data1)->save()) !==false){
					return true;
				}
			}
		}
		return false;
	}
}

